package org.example.test_03.Service.Impl;

import org.example.test_03.Domian.ReturnData;
import org.example.test_03.Domian.User;
import org.example.test_03.Mapper.UserMapper;
import org.example.test_03.Service.UserService;
import org.example.test_03.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 实现类
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ReturnData login(User user) {
        User user1 = userMapper.getUserByUsername(user.getUsername());
        if(user1==null){
            return ReturnData.error("没有这个用户");
        }
        if(!user1.getPassword().equals(user.getPassword()) ){
            return ReturnData.error("密码错误");
        }
        HashMap<String,Object> map = new HashMap();
        String token = JWTUtil.createToken(user1.getId());
        map.put("token",token);
        map.put("adminInfo",user1);
        return ReturnData.ok(map);
    }
}
